﻿using CefSharp;
using CefSharp.Handler;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CefSharpWinformDemo
{
    public class MyResourceRequestHandler : ResourceRequestHandler
    {
        private string _token = string.Empty;

        public MyResourceRequestHandler(string token)
        {
            _token = token;
        }

        protected override CefReturnValue OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
        {
            if (_token != null)
            {
                Uri url;
                if (Uri.TryCreate(request.Url, UriKind.Absolute, out url) == false)
                {
                    return CefReturnValue.Cancel;
                }
                var headers = request.Headers;
                headers["token"] = _token; //传递进去认证Token
                request.Headers["token"] = _token;
                request.Headers = headers;
                return CefReturnValue.Continue;
            }
            return CefReturnValue.Continue;
        }
    }
}
